package com.right.health.app.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

import com.right.health.app.R;

/*
 * Created by hopeliao on 2019-09-25
 *
 */
public class MeScrollView extends ScrollView {


    private int mOffset;
    private int mLastOffset;

    public MeScrollView(Context context) {
        super(context);
        init();
    }

    public MeScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MeScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MeScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mOffset = getResources().getDimensionPixelOffset(R.dimen.dp_94);
    }

    private int mScrollY;

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        mScrollY = t;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        if(action == MotionEvent.ACTION_DOWN) {
            int downY = (int)ev.getY();

            if(downY < mOffset) {
                if(mScrollY < mLastOffset) {
                    mLastOffset = mOffset - mScrollY;
                    return false;
                } else {
                    mLastOffset = mOffset;
                }
            } else  {
                mLastOffset = mOffset;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {


        return super.onTouchEvent(ev);
    }
}
